#include "CoCoA/library.H"
#include <iostream>
#include <cstdlib>
#include <map>
using namespace CoCoA;
using namespace std;


static map<size_t, SparsePolyRing> __cocoa_rings;

SparsePolyRing create_ring(size_t i) {
		map<size_t, SparsePolyRing>::iterator pos = __cocoa_rings.find(i);
		if (pos != __cocoa_rings.end())
				return pos->second;
		else
		{
			pair< map<size_t, SparsePolyRing>::iterator, bool> res = __cocoa_rings.insert(make_pair(i,NewPolyRing(RingQ(), i)));
			return (res.first)->second;
		}

		/*
		try
		{
				return __cocoa_rings[i];
		}
		catch (...)
		{
			__cocoa_rings.insert(make_pair(i,SparsePolyRing(RingQ(), i)));
			return __cocoa_rings[i];
		}
		*/
}

class Wrapper {
		public:
				Wrapper(const RingElem& poly):__flag('C'){
					__data=new RingElem(poly);
				}

				Wrapper(size_t Ri, size_t i, char flag='C'):__flag(flag) {
					switch (__flag) {
						case 'C':
							__data=new RingElem(indet(create_ring(Ri),i-1));
							break;
					}
				}

				~Wrapper() {
					switch (__flag) {
						case 'C':
							delete (RingElem*) __data;
							break;
						}
				}

		void print() {
			cout << *(RingElem*)__data << endl;
		}

		private:
				void* __data;
				char __flag;
};


int main(){
	CoCoA::GlobalManager CoCoAFoundations;
	srand((unsigned long)&CoCoAFoundations);
	SparsePolyRing R = NewPolyRing(RingQ(), symbols("x","y"));
	Wrapper w(indet(R,1)), w1(2,2);
	w.print();
	w1.print();
	// delete p; error
	Wrapper w2(3,2), w3(3,3);
	w2.print();
	w3.print();
	return 0;
}
